package ${packagePrefix}.infrastructure.mapper;

import ${packagePrefix}.client.dto.query.${domainObjectName}ListQry;
import ${packagePrefix}.infrastructure.dataobject.${domainObjectName}DO;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
<#if hasDateFields>
import java.util.Date;
</#if>
<#if hasDecimalFields>
import java.math.BigDecimal;
</#if>

import static org.junit.jupiter.api.Assertions.*;

/**
 * ${domainObjectName}Mapper 测试类
 */
@SpringBootTest
@Transactional
@DisplayName("${entityComment}Mapper测试")
class ${domainObjectName}MapperTest {

    @Autowired
    private ${domainObjectName}Mapper ${domainObjectName?uncap_first}Mapper;

    @Test
    @DisplayName("测试插入${entityComment}")
    void testInsert() {
        // 准备测试数据
        ${domainObjectName}DO ${domainObjectName?uncap_first}DO = createTestDO();
        
        // 执行插入
        int result = ${domainObjectName?uncap_first}Mapper.insert(${domainObjectName?uncap_first}DO);
        
        // 验证结果
        assertEquals(1, result);
        assertNotNull(${domainObjectName?uncap_first}DO.getId());
        
        // 验证可以查询到
        ${domainObjectName}DO queried = ${domainObjectName?uncap_first}Mapper.selectById(${domainObjectName?uncap_first}DO.getId());
        assertNotNull(queried);
        assertEquals(${domainObjectName?uncap_first}DO.getId(), queried.getId());
<#list fields as field>
<#if field.name != "id">
        assertEquals(${domainObjectName?uncap_first}DO.get${field.name?cap_first}(), queried.get${field.name?cap_first}());
</#if>
</#list>
    }

    @Test
    @DisplayName("测试根据ID查询${entityComment}")
    void testSelectById() {
        // 准备测试数据
        ${domainObjectName}DO ${domainObjectName?uncap_first}DO = createTestDO();
        ${domainObjectName?uncap_first}Mapper.insert(${domainObjectName?uncap_first}DO);
        
        // 执行查询
        ${domainObjectName}DO result = ${domainObjectName?uncap_first}Mapper.selectById(${domainObjectName?uncap_first}DO.getId());
        
        // 验证结果
        assertNotNull(result);
        assertEquals(${domainObjectName?uncap_first}DO.getId(), result.getId());
<#list fields as field>
<#if field.name != "id">
        assertEquals(${domainObjectName?uncap_first}DO.get${field.name?cap_first}(), result.get${field.name?cap_first}());
</#if>
</#list>
    }

    @Test
    @DisplayName("测试更新${entityComment}")
    void testUpdateById() {
        // 准备测试数据
        ${domainObjectName}DO ${domainObjectName?uncap_first}DO = createTestDO();
        ${domainObjectName?uncap_first}Mapper.insert(${domainObjectName?uncap_first}DO);
        
        // 修改数据
<#list fields as field>
<#if field.name != "id">
<#if field.javaType == "String">
        ${domainObjectName?uncap_first}DO.set${field.name?cap_first}("Updated${field.name?cap_first}");
<#elseif field.javaType == "Integer" || field.javaType == "Long">
        ${domainObjectName?uncap_first}DO.set${field.name?cap_first}(${domainObjectName?uncap_first}DO.get${field.name?cap_first}() + 1);
<#elseif field.javaType == "BigDecimal">
        ${domainObjectName?uncap_first}DO.set${field.name?cap_first}(${domainObjectName?uncap_first}DO.get${field.name?cap_first}().add(new BigDecimal("1.0")));
<#elseif field.javaType == "Boolean">
        ${domainObjectName?uncap_first}DO.set${field.name?cap_first}(!${domainObjectName?uncap_first}DO.get${field.name?cap_first}());
<#elseif field.javaType == "Date">
        ${domainObjectName?uncap_first}DO.set${field.name?cap_first}(new Date());
</#if>
</#if>
</#list>
        
        // 执行更新
        int result = ${domainObjectName?uncap_first}Mapper.updateById(${domainObjectName?uncap_first}DO);
        
        // 验证结果
        assertEquals(1, result);
        
        // 验证更新成功
        ${domainObjectName}DO updated = ${domainObjectName?uncap_first}Mapper.selectById(${domainObjectName?uncap_first}DO.getId());
        assertNotNull(updated);
<#list fields as field>
<#if field.name != "id">
        assertEquals(${domainObjectName?uncap_first}DO.get${field.name?cap_first}(), updated.get${field.name?cap_first}());
</#if>
</#list>
    }

    @Test
    @DisplayName("测试根据条件查询${entityComment}列表")
    void testSelectByCondition() {
        // 准备测试数据
        ${domainObjectName}DO ${domainObjectName?uncap_first}DO1 = createTestDO();
        ${domainObjectName?uncap_first}Mapper.insert(${domainObjectName?uncap_first}DO1);
        
        ${domainObjectName}DO ${domainObjectName?uncap_first}DO2 = createTestDO();
<#list fields as field>
<#if field.name != "id">
<#if field.javaType == "String">
        ${domainObjectName?uncap_first}DO2.set${field.name?cap_first}("Different${field.name?cap_first}");
<#elseif field.javaType == "Integer" || field.javaType == "Long">
        ${domainObjectName?uncap_first}DO2.set${field.name?cap_first}(${domainObjectName?uncap_first}DO2.get${field.name?cap_first}() + 10);
<#elseif field.javaType == "BigDecimal">
        ${domainObjectName?uncap_first}DO2.set${field.name?cap_first}(${domainObjectName?uncap_first}DO2.get${field.name?cap_first}().add(new BigDecimal("10.0")));
<#elseif field.javaType == "Boolean">
        ${domainObjectName?uncap_first}DO2.set${field.name?cap_first}(!${domainObjectName?uncap_first}DO2.get${field.name?cap_first}());
</#if>
</#if>
</#list>
        ${domainObjectName?uncap_first}Mapper.insert(${domainObjectName?uncap_first}DO2);
        
        // 执行条件查询（这里仅测试基本查询，实际使用时应根据业务逻辑设置查询条件）
        ${domainObjectName}ListQry qry = new ${domainObjectName}ListQry();
        List<${domainObjectName}DO> results = ${domainObjectName?uncap_first}Mapper.selectByCondition(qry);
        
        // 验证结果
        assertNotNull(results);
        assertTrue(results.size() >= 2);
        assertTrue(results.stream().anyMatch(item -> item.getId().equals(${domainObjectName?uncap_first}DO1.getId())));
        assertTrue(results.stream().anyMatch(item -> item.getId().equals(${domainObjectName?uncap_first}DO2.getId())));
    }

    @Test
    @DisplayName("测试删除${entityComment}")
    void testDeleteById() {
        // 准备测试数据
        ${domainObjectName}DO ${domainObjectName?uncap_first}DO = createTestDO();
        ${domainObjectName?uncap_first}Mapper.insert(${domainObjectName?uncap_first}DO);
        
        // 确认数据存在
        assertNotNull(${domainObjectName?uncap_first}Mapper.selectById(${domainObjectName?uncap_first}DO.getId()));
        
        // 执行删除
        int result = ${domainObjectName?uncap_first}Mapper.deleteById(${domainObjectName?uncap_first}DO.getId());
        
        // 验证结果
        assertEquals(1, result);
        assertNull(${domainObjectName?uncap_first}Mapper.selectById(${domainObjectName?uncap_first}DO.getId()));
    }
    
    /**
     * 创建测试用${entityComment}DO对象
     */
    private ${domainObjectName}DO createTestDO() {
        ${domainObjectName}DO ${domainObjectName?uncap_first}DO = new ${domainObjectName}DO();
<#list fields as field>
<#if field.name != "id">
<#if field.javaType == "String">
        ${domainObjectName?uncap_first}DO.set${field.name?cap_first}("Test${field.name?cap_first}");
<#elseif field.javaType == "Integer">
        ${domainObjectName?uncap_first}DO.set${field.name?cap_first}(123);
<#elseif field.javaType == "Long">
        ${domainObjectName?uncap_first}DO.set${field.name?cap_first}(123L);
<#elseif field.javaType == "BigDecimal">
        ${domainObjectName?uncap_first}DO.set${field.name?cap_first}(new BigDecimal("123.45"));
<#elseif field.javaType == "Boolean">
        ${domainObjectName?uncap_first}DO.set${field.name?cap_first}(true);
<#elseif field.javaType == "Date">
        ${domainObjectName?uncap_first}DO.set${field.name?cap_first}(new Date());
</#if>
</#if>
</#list>
        return ${domainObjectName?uncap_first}DO;
    }
} 